package com.cr.admin.business.order.dao.repository;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cr.admin.business.order.dao.mapper.OrderMapper;
import com.cr.admin.business.order.pojo.entity.Order;
import com.cr.admin.business.order.pojo.param.OrderSaveParam;
import com.cr.admin.business.order.pojo.param.OrderUpdateParam;
import com.cr.admin.business.order.pojo.query.OrderQuery;
import com.cr.common.exception.ServiceException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Random;

@Repository
public class OrderRepositoryImpl implements OrderRepository{
    @Autowired
    private OrderMapper orderMapper;
    @Override
    public List<Order> selectList(OrderQuery orderQuery) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();

        if (orderQuery.getId() != null) {
            // 执行操作，比如设置查询条件
            queryWrapper.like("id", orderQuery.getId());
        }

        if(orderQuery.getUserId()!=null){
            queryWrapper.like("user_id",orderQuery.getUserId());
        }

        return orderMapper.selectList(queryWrapper);
    }

    @Override
    public void insert(OrderSaveParam orderSaveParam) {
        Order order = new Order();
        BeanUtils.copyProperties(orderSaveParam,order);

        // 生成随机订单号，假设你希望订单号保持唯一性
//        Long orderNumber = generateUniqueOrderNumber();
//        order.setOrderId(orderNumber);

        // 设置订单时间为当前时间
        order.setCreateTime(new Date());

        //设置默认状态为1
//        order.setOrderStatus("1");

        //根据传过来的日期与时间戳租车,起始时间至租车结束时间计算出租车时长
        long rentalDuration = calculateRentalDuration(orderSaveParam.getStartDate(), orderSaveParam.getEndDate());
        order.setRentalDuration(rentalDuration);

        //设置支付状态默认为0
//        order.setPaymentStatus("0");

        orderMapper.insert(order);
    }

    @Override
    public Order selectById(Long id) {
        return orderMapper.selectById(id);
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            throw new ServiceException("参数不能为空");
        }
        orderMapper.deleteBatchIds(ids);
    }

    @Override
    public void updateById(OrderUpdateParam orderUpdateParam) {
        Order order = new Order();
        BeanUtils.copyProperties(orderUpdateParam,order);
        orderMapper.updateById(order);
    }


    // 生成唯一的订单号
    private Long generateUniqueOrderNumber() {
        // 这里可以根据业务需求生成唯一的订单号，例如使用数据库的序列、UUID、时间戳+随机数等方式生成
        Random random = new Random();
        long currentTime = System.currentTimeMillis();
        long orderNumber = currentTime * 1000 + random.nextInt(1000); // 保证足够唯一性
        return orderNumber;
    }

    // 计算租车时长的方法，假设 startTime 和 endTime 是 Date 类型
    private long calculateRentalDuration(Date startTime, Date endTime) {
        long startMillis = startTime.getTime();
        long endMillis = endTime.getTime();
        return (endMillis - startMillis) / (1000 * 60 * 60); // 返回租车时长，单位为小时
    }
}
